package eleanor.ltd.isRealEstate.pages.estate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.SocketException;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.upload.services.UploadedFile;

import eleanor.ltd.isRealEstate.annotations.BuyerAccess;
import eleanor.ltd.isRealEstate.util.Properties;

@BuyerAccess
public class AddEstate {

	@Property
	private UploadedFile uploadFile;
	
	@Persist
	private FTPClient ftpc;
	
	
	void onPrepare() {
		ftpc = new FTPClient();
	}
	
	public void onSuccess() {
		File file = new File("C:\\Users\\eleanor\\upload\\2\\" + uploadFile.getFileName());

		uploadFile.write(file);
		System.out.println("Name of SELECTED FILE: " + uploadFile.getFileName());
		
		uploadOnFTP(file);
		
		
		
		
	}
	
	Object onUploadException(FileUploadException fux) {
		System.out.println("Upload exception:");
		System.out.println(fux.getLocalizedMessage());
		System.out.println(fux.getStackTrace());
		System.out.println("----------------------------------------------------------------------");
		
		return this;
	}
	
	private void uploadOnFTP(File file) {
//		FTPClient ftpc = new FTPClient();
		int reply;
		FileInputStream in;
		
		String address = Properties.FTP_ADDRESS;
		String username = Properties.FTP_USER;
		String password = Properties.FTP_PASS;
		
		try {
			ftpc.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
			ftpc.connect(address);
			
			ftpc.login(username, password);
			ftpc.enterLocalPassiveMode();
			System.out.println("After connect: " + ftpc.printWorkingDirectory());
			boolean workDir = ftpc.changeWorkingDirectory("/public_html/tapestry");
			System.out.println("After change: " + ftpc.printWorkingDirectory());
			
			FTPFile[] ff = ftpc.listFiles();
			for(int i = 0; i < ff.length; i++) {
				System.out.println("FAJL (" + i + "): " + ff[i].getName());
			}
			
			reply = ftpc.getReplyCode();
			
			if(FTPReply.isPositiveCompletion(reply)) {
				System.out.println("FTP Konekcija uspjesno ostvarena.");
				
				System.out.println("String fileDir: " + file );
				System.out.println("Name of the file: " + file.getName());
				//Upload fajl-a
				in = new FileInputStream(file);
				
				ftpc.storeFile(file.getName(), in);
				
				System.out.println("Fajl uspjesno uploadovan!");
				
			} else {
				System.out.println("FTP Konekcija neuspjesna!");
				ftpc.disconnect();
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
